package com.aegis.gateway.filter;

import com.aegis.gateway.constants.FilterChainEnum;
import com.aegis.gateway.service.FilterManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@Slf4j
@Component
public class BeforeWebFilter implements WebFilter, Ordered {

    @Autowired
    protected FilterManageService filterManageService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        log.info("===> before filter start <===");
        if (exchange.getRequest().getMethod().matches("OPTIONS")) {
            filterManageService.filterChainEnd(exchange);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return FilterChainEnum.BEFORE_FILTER.getOrder();
    }

}
